package com.yodawnla.katana.Hud;

import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.modifier.MoveXModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.ease.EaseBounceOut;

import com.yodawnla.katana.Player;
import com.yodawnla.katana.Hud.HudMgr.Hud;
import com.yodawnla.katana.system.Fonts;
import com.yodawnla.katana.system.Tex;
import com.yodawnla.lib.YoActivity;
import com.yodawnla.lib.util.widget.YoHudMgr;
import com.yodawnla.lib.util.widget.YoText;

public class TalkHud extends AHud
{
	public static TalkHud getInstance()
	{
		if (instance == null)
			instance = new TalkHud();
		return instance;
	}
	
	static TalkHud instance;
	
	YoActivity mBase = YoActivity.getBaseActivity();
	
	Entity mCharLayer;
	
	
	Sprite mMyTalk;
	Sprite mEnemyTalk;
	Sprite mTalk;
	YoText mTalkText;
	
	YoText mMyName;
	YoText mEnemyName;
	
	YoText mMyTitle;
	YoText mEnemyTitle;
	
	Player mPlayer = Player.getInstance();
	
	public interface ITalkHudListener
	{
		public void onHudInitFinish();
	}

	TalkHud()
	{
		super(Hud.Talk);
		super.hide();
		Entity layer = YoHudMgr.getInstance().getHudLayer(mName);
		
		mCharLayer = new Entity();
		layer.attachChild(mCharLayer);
		
		mTalk = new Sprite(148, 13, mBase.getTexture(Tex.Talk));
		layer.attachChild(mTalk);
		
		mTalkText = new YoText(48, 43, Fonts.WHITE25, 100);
		mTalkText.setColor(0, 0, 0);
		mTalk.attachChild(mTalkText);
		
		mMyName = new YoText(20, 420, Fonts.SuFaW30, 20);
		layer.attachChild(mMyName);
		mMyName.setScaleCenterX(0);
		mMyName.setScale(1.5f);
		
		mMyTitle = new YoText(20, 380, Fonts.SuFaW30, 10);
		mMyTitle.setScaleCenterX(0);
		mMyTitle.setScale(1.2f);
		layer.attachChild(mMyTitle);
		
		mEnemyName = new YoText(0, 420, Fonts.SuFaW30, 20);
		layer.attachChild(mEnemyName);
		mEnemyName.setScale(1.5f);
		mEnemyName.setScaleCenterX(0);
		mEnemyName.setPosition(780 - mEnemyName.getWidthScaled(), 420);
		
		mEnemyTitle = new YoText(20, 380, Fonts.SuFaW30, 10);
		mEnemyTitle.setScaleCenterX(0);
		mEnemyTitle.setScale(1.2f);
		mEnemyTitle.setPosition(780 - mEnemyTitle.getWidthScaled(), 380);
		layer.attachChild(mEnemyTitle);
	}
	
	public void initTalk(String enemyName, String enemyTitle, final int enemyFaceType, final ITalkHudListener listener)
	{
		super.show();
		mTalk.setVisible(false);
		mEnemyName.setText(enemyName);
		mEnemyTitle.setText(enemyTitle);
		mMyName.setText(mPlayer.getName());
		mMyTitle.setText(mPlayer.getTitleString());
		
		mEnemyTitle.setPosition(780 - mEnemyTitle.getWidthScaled(), 380);
		mEnemyName.setPosition(780 - mEnemyName.getWidthScaled(), 420);
		
		mMyName.setVisible(false);
		mMyTitle.setVisible(false);
		mEnemyName.setVisible(false);
		mEnemyTitle.setVisible(false);
		
		_fixTextPosition();
		
		mBase.runOnUpdateThread(new Runnable()
		{
			public void run()
			{
				if (mMyTalk != null)
				{
					mMyTalk.setPosition(-10000, -10000);
					mMyTalk.setVisible(false);
					mMyTalk.clearEntityModifiers();
					mMyTalk.detachSelf();
				}
				
				mMyTalk = new Sprite(0, 0, 500, 477, mBase.getTexture(Tex.Char+mPlayer.getCharType()).deepCopy());
				//mMyTalk.setFlippedHorizontal(true);
				mCharLayer.attachChild(mMyTalk);
				
				if (mEnemyTalk != null)
				{
					mEnemyTalk.setPosition(-10000, -10000);
					mEnemyTalk.setVisible(false);
					mEnemyTalk.clearEntityModifiers();
					mEnemyTalk.detachSelf();
				}
				
				mEnemyTalk = new Sprite(0, 0, 423, 403, mBase.getTexture(Tex.Char+enemyFaceType).deepCopy());
				mEnemyTalk.setPosition(800 - mEnemyTalk.getWidth() +2, -2);
				mEnemyTalk.setFlippedHorizontal(true);
				mCharLayer.attachChild(mEnemyTalk);
				mEnemyTalk.setColor(0.3f, 0.3f, 0.3f);
				
//				mCharLayer.setVisible(false);
				mMyTalk.registerEntityModifier(new MoveXModifier(0.8f, -1000, 0));
				mEnemyTalk.registerEntityModifier(new MoveXModifier(0.8f, 2000, 800 - mEnemyTalk.getWidth() +2, new IEntityModifierListener()
				{
					
					public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
					{
					}
					
					public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem)
					{
						listener.onHudInitFinish();
					}
				}));

			}
		});
	}
	
	public void showTalk(boolean me, String text)
	{
//		super.show();
//		Log.e("TalkHud", "showTalk " + text);
//		mCharLayer.setVisible(true);
		mTalk.setVisible(true);
		
		mTalk.registerEntityModifier(new ScaleModifier(0.5f, 0.2f, 1, EaseBounceOut.getInstance()));
		
		mTalkText.setText(text);
		if (me)
		{
			mTalk.setPosition(248, mTalk.getY());
			mTalk.setFlippedHorizontal(false);
//			Log.e("HudMgr", "showTalk ME");
			mMyTalk.setSize(500, 477);
			mMyTalk.setColor(1, 1, 1);
			
			mEnemyTalk.setSize(423, 403);
			mEnemyTalk.setPosition(800 - mEnemyTalk.getWidth() +2, -2);
			mEnemyTalk.setColor(0.3f, 0.3f, 0.3f);
			
			mMyName.setVisible(true);
			mMyTitle.setVisible(true);
			mEnemyName.setVisible(false);
			mEnemyTitle.setVisible(false);
		}
		else 
		{
			mTalk.setPosition(48, mTalk.getY());
			mTalk.setFlippedHorizontal(true);
//			Log.e("HudMgr", "showTalk OP");
			mMyTalk.setSize(423, 403);
			mMyTalk.setColor(0.3f, 0.3f, 0.3f);
			
			mEnemyTalk.setSize(500, 477);
			mEnemyTalk.setPosition(800 - mEnemyTalk.getWidth() +2, -2);
			mEnemyTalk.setColor(1, 1, 1);
			
			mMyName.setVisible(false);
			mMyTitle.setVisible(false);
			mEnemyName.setVisible(true);
			mEnemyTitle.setVisible(true);
		}
	}
	
	public void hide()
	{
		mMyName.setVisible(false);
		mMyTitle.setVisible(false);
		mEnemyName.setVisible(false);
		mEnemyTitle.setVisible(false);
		mTalk.setVisible(false);
		
		mMyTalk.registerEntityModifier(new MoveXModifier(0.8f, 0, -1000));
		mEnemyTalk.registerEntityModifier(new MoveXModifier(0.8f, 800 - mEnemyTalk.getWidth() +2, 2000, new IEntityModifierListener()
		{
			
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
			{
			}
			
			public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem)
			{
				TalkHud.super.hide();
			}
		}));
	}
	
	private void _fixTextPosition()
	{
		mMyName.setScaleCenterX(0);
		mMyName.setScale(1.5f);
		
		mMyTitle.setScaleCenterX(0);
		mMyTitle.setScale(1.2f);
		
		mEnemyName.setScale(1.5f);
		mEnemyName.setScaleCenterX(0);
		mEnemyName.setPosition(780 - mEnemyName.getWidthScaled(), 420);
	}
}
